package com.sinooceangroup.capital.consumer.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.sinooceangroup.capital.consumer.service.HystrixService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * {@link HystrixService} 接口实现
 *
 * @author: Leo.Wang, adwyxx@qq.com
 * @date: 2020/7/20 18:33
 */
@Service
public class HystrixServiceImpl implements HystrixService {
    //=====服务熔断
    @HystrixCommand(fallbackMethod = "circuitBreaker_fallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), // 时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),// 失败率达到多少后跳闸
    })
    @Override
    public String circuitBreaker(@PathVariable("id") Integer id)
    {
        if(id < 0) {
            throw new RuntimeException("******id 不能负数");
        }
        String serialNumber = UUID.randomUUID().toString();

        return LocalDateTime.now().toString()+": 调用成功，流水号: " + serialNumber;
    }

    public String circuitBreaker_fallback(Integer id){
        return  LocalDateTime.now().toString()+": circuitBreaker_fallback:"+id;
    }
}
